﻿
namespace SPL.Windows.Forms
{
	/// <summary>
	/// abstract Status typed datagridview
	/// </summary>
	[System.ComponentModel.DesignerCategoryAttribute("code")]
	public abstract class StatusDataGridViewBase : System.Windows.Forms.UserControl
	{	
		/// <summary> 
		/// Thrown when the current Entity changed.
		/// </summary>
		public event System.EventHandler<StatusDataGridViewEventArgs> CurrentEntityChanged;
		
		#region Fields
				
		/// <summary> 
		/// the current entity.
		/// </summary>
		private Entities.Status _currentStatus = null;
		//private Dictionary<string, string> _columnsHeaders = new Dictionary<string, string>();
		
		/// <summary> 
		/// the datagridview instance
		/// </summary>
		protected System.Windows.Forms.DataGridView uxStatusDataGridView;
		
		/// <summary> 
		/// the errorprovider
		/// </summary>
		protected System.Windows.Forms.ErrorProvider uxStatusErrorProvider;
		
		/// <summary> 
		/// the bindingsource
		/// </summary>
		protected System.Windows.Forms.BindingSource uxStatusBindingSource;
		
		
		/// <summary> 
		/// the DGV column associated with the Id property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxIdDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the Name property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxNameDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the Value property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxValueDataGridViewColumn;
		
		#endregion
					
		#region Combo list
		
		#endregion
		
		#region Main Datasource
		
		private Entities.TList<Entities.Status> _StatusList;
				
		/// <summary> 
		/// The list of Status to be displayed in the DGV.
		/// </summary>
		[System.ComponentModel.Category("Data")]
		public Entities.TList<Entities.Status> StatusList
		{
			get {return this._StatusList;}
			set
			{
				this._StatusList = value;
				this.uxStatusBindingSource.DataSource = null;
				this.uxStatusBindingSource.DataSource = value;
				this.uxStatusDataGridView.DataSource = null;
				this.uxStatusDataGridView.DataSource = this.uxStatusBindingSource;				
				//this.uxStatusBindingSource.ListChanged += new System.ComponentModel.ListChangedEventHandler(uxStatusBindingSource_ListChanged);
				this.uxStatusBindingSource.CurrentItemChanged += new System.EventHandler(OnStatusBindingSourceCurrentItemChanged);
			}
		}
		
		private void OnStatusBindingSourceCurrentItemChanged(object sender, System.EventArgs e)
		{
			_currentStatus = uxStatusBindingSource.Current as Entities.Status;
			
			if (_currentStatus != null)
			{
				_currentStatus.Validate();
			}
			//_Status.Validate();
			OnCurrentEntityChanged();
		}

		//void uxStatusBindingSource_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
		//{
			//throw new Exception("The method or operation is not implemented.");
		//}		
		
		#endregion
		
		/// <summary>
		/// Gets the currently selected <c cref="Entities.Status"/> instance.
		/// </summary>
		public Entities.Status SelectedStatus
		{
			get {return this._currentStatus;}
		}
		
		#region other properties
		
		/// <summary>
		/// Gets or sets a value indicating whether you have provided your own data-management operations for the DataGridView control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool VirtualMode
		{
			get {return this.uxStatusDataGridView.VirtualMode;}
			set
			{
				this.uxStatusDataGridView.VirtualMode = value;
			}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the option to add rows is displayed to the user.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToAddRows
		{
			get {return this.uxStatusDataGridView.AllowUserToAddRows;}
			set {this.uxStatusDataGridView.AllowUserToAddRows = value;}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the user is allowed to delete rows from the DataGridView.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToDeleteRows
		{
			get {return this.uxStatusDataGridView.AllowUserToDeleteRows;}
			set {this.uxStatusDataGridView.AllowUserToDeleteRows = value;}
		}
		
		/// <summary>
		/// Gets the <c cref="DataGridView"/> instance.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public System.Windows.Forms.DataGridView DataGridView
		{
			get {return this.uxStatusDataGridView;}
		}
		
		/// <summary>
		/// Gets a collection that contains all the columns in the control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]		
		[System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Visible)]
		//[System.ComponentModel.EditorAttribute("System.Windows.Forms.Design.DataGridViewColumnCollectionEditor, System.Design", typeof(System.Drawing.Design.UITypeEditor))] 
		public System.Windows.Forms.DataGridViewColumnCollection Columns
		{
			get { return this.uxStatusDataGridView.Columns; }
		}
		
		#endregion
		
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;
	
		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}
	
		#region Constructor
	
		/// <summary>
		/// Initializes a new instance of the <see cref="StatusDataGridViewBase"/> class.
		/// </summary>
		public StatusDataGridViewBase()
		{			
			this.components = new System.ComponentModel.Container();			
			
			this.uxStatusDataGridView = new System.Windows.Forms.DataGridView();
			this.uxStatusBindingSource = new System.Windows.Forms.BindingSource(this.components);
			this.uxStatusErrorProvider = new System.Windows.Forms.ErrorProvider(this.components);
			this.uxIdDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxNameDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxValueDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			((System.ComponentModel.ISupportInitialize)(this.uxStatusDataGridView)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxStatusBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxStatusErrorProvider)).BeginInit();
			this.SuspendLayout();
						
			// 
			// uxStatusErrorProvider
			// 
			this.uxStatusErrorProvider.ContainerControl = this;
			this.uxStatusErrorProvider.DataSource = this.uxStatusBindingSource;						
			// 
			// uxStatusDataGridView
			// 
			this.uxStatusDataGridView.AutoGenerateColumns = false;
			this.uxStatusDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.uxStatusDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
		this.uxIdDataGridViewColumn,
		this.uxNameDataGridViewColumn,
		this.uxValueDataGridViewColumn			});
			this.uxStatusDataGridView.Dock = System.Windows.Forms.DockStyle.Fill;
			this.uxStatusDataGridView.Location = new System.Drawing.Point(0, 0);
			this.uxStatusDataGridView.Name = "uxStatusDataGridView";
			this.uxStatusDataGridView.Size = new System.Drawing.Size(470, 300);
			this.uxStatusDataGridView.TabIndex = 0;	
			this.uxStatusDataGridView.BackgroundColor = System.Drawing.Color.WhiteSmoke;
			this.uxStatusDataGridView.EnableHeadersVisualStyles = false;
			this.uxStatusDataGridView.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.OnStatusDataGridViewDataError);
			this.uxStatusDataGridView.CellValueNeeded += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnStatusDataGridViewCellValueNeeded);
			this.uxStatusDataGridView.CellValuePushed += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnStatusDataGridViewCellValuePushed);
			
			//
			// uxIdDataGridViewColumn
			//
			this.uxIdDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIdDataGridViewColumn.DataPropertyName = "Id";
			this.uxIdDataGridViewColumn.HeaderText = "Id";
			this.uxIdDataGridViewColumn.Name = "uxIdDataGridViewColumn";
			this.uxIdDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIdDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIdDataGridViewColumn.ReadOnly = false;		
			//
			// uxNameDataGridViewColumn
			//
			this.uxNameDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxNameDataGridViewColumn.DataPropertyName = "Name";
			this.uxNameDataGridViewColumn.HeaderText = "Name";
			this.uxNameDataGridViewColumn.Name = "uxNameDataGridViewColumn";
			this.uxNameDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxNameDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxNameDataGridViewColumn.ReadOnly = false;		
			//
			// uxValueDataGridViewColumn
			//
			this.uxValueDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxValueDataGridViewColumn.DataPropertyName = "Value";
			this.uxValueDataGridViewColumn.HeaderText = "Value";
			this.uxValueDataGridViewColumn.Name = "uxValueDataGridViewColumn";
			this.uxValueDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxValueDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxValueDataGridViewColumn.ReadOnly = false;		
			
			// this
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.Controls.Add(this.uxStatusDataGridView);
			this.Name = "StatusDataGridView";
			this.Size = new System.Drawing.Size(470, 300);
			((System.ComponentModel.ISupportInitialize)(this.uxStatusErrorProvider)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxStatusDataGridView)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxStatusBindingSource)).EndInit();
			this.ResumeLayout(false);
		}
		#endregion
				
		#region events
		
		/// <summary>
		/// Raised the CurrentEntityChanged event.
		/// </summary>
		protected void OnCurrentEntityChanged()
		{
			if (CurrentEntityChanged != null)
			{
				StatusDataGridViewEventArgs args = new StatusDataGridViewEventArgs();
				args.Status = _currentStatus;
				CurrentEntityChanged(this, args); 
			}
		}
		
		#endregion
		
		/// <summary>
		/// Strongly typed event 
		/// </summary>
		public class StatusDataGridViewEventArgs : System.EventArgs
		{
			private Entities.Status	_Status;
	
			/// <summary>
			/// the  Entities.Status instance.
			/// </summary>
			public Entities.Status Status
			{
				get { return _Status; }
				set { _Status = value; }
			}	
		}
		
		/// <summary>
		/// Handles the DataError event of the uxStatusDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewDataErrorEventArgs"/> instance containing the event data.</param>
		protected void OnStatusDataGridViewDataError(object sender, System.Windows.Forms.DataGridViewDataErrorEventArgs e)
		{
			System.Diagnostics.Debug.WriteLine(string.Format( "{0}, {1}", e.ColumnIndex, e.RowIndex));
		}
		
		/// <summary>
		/// Handles the CellValueNeeded event of the uxStatusDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnStatusDataGridViewCellValueNeeded(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxStatusDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIdDataGridViewColumn":
						e.Value = StatusList[e.RowIndex].Id;
						break;
					case "uxNameDataGridViewColumn":
						e.Value = StatusList[e.RowIndex].Name;
						break;
					case "uxValueDataGridViewColumn":
						e.Value = StatusList[e.RowIndex].Value;
						break;
				default:
				break;
			}
		}
		
		/// <summary>
		/// Handles the CellValuePushed event of the uxStatusDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnStatusDataGridViewCellValuePushed(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxStatusDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIdDataGridViewColumn":
						StatusList[e.RowIndex].Id = (System.Int32)e.Value;
						break;
					case "uxNameDataGridViewColumn":
						StatusList[e.RowIndex].Name = (System.String)e.Value;
						break;
					case "uxValueDataGridViewColumn":
						StatusList[e.RowIndex].Value = (System.Int32)e.Value;
						break;
				default:
				break;
			}
		}
	}
}
